package com.ctd.springframework.core.io.support;

import com.ctd.springframework.core.io.InputStreamSource;
import com.ctd.springframework.core.io.Resource;
import com.ctd.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;

/**
 * EncodedResource
 *
 * @author chentudong
 * @time 2019/3/24 21:48
 */
public class EncodedResource implements InputStreamSource {
    /**
     * spring 自定义读取文件源
     */
    private final Resource resource;

    /**
     * 编码
     */
    private final String encoding;

    public EncodedResource(Resource resource) {
        this(resource, null);
    }

    public EncodedResource(Resource resource, String encoding) {
        super();
        Assert.notNull(resource, "Resource must not be null");
        this.resource = resource;
        this.encoding = encoding;
    }

    /**
     * getEncoding
     *
     * @return String
     */
    public String getEncoding() {
        return encoding;
    }

    /**
     * getResource
     *
     * @return Resource
     */
    public final Resource getResource() {
        return this.resource;
    }

    /**
     * getInputStream
     * 获取resource的输入流
     *
     * @return InputStream
     * @throws IOException IOException
     */
    @Override
    public InputStream getInputStream() throws IOException {
        return this.resource.getInputStream();
    }
}
